package com.itheima.huikebackend.controller;

import com.itheima.huikebackend.common.PageResult;
import com.itheima.huikebackend.common.R;
import com.itheima.huikebackend.common.Result;
import com.itheima.huikebackend.dto.SysUserDTO;
import com.itheima.huikebackend.dto.SysUserPageDTO;
import com.itheima.huikebackend.dto.SysUserUpdateDTO;
import com.itheima.huikebackend.service.SysUserService;
import com.itheima.huikebackend.vo.SysUserEchoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @program: huike
 * @className: UserController
 * @description: TODO
 * @author: xch
 * @createTime: 2023-08-07 20:11
 **/
@RestController
@RequestMapping("/system/user")
public class SysUserController {

    @Autowired
    private SysUserService sysuserService;

    /**
     * Description 用户查询
     * @param userPageDTO
     * @return com.itheima.huikebackend.common.R<com.itheima.huikebackend.common.PageResult>
     * Date 2023/8/7
     */
    @GetMapping("/list")
    public Map<String, Object> selectUserByPage(SysUserPageDTO userPageDTO){
        return Result.successMap(sysuserService.selectUserByPage(userPageDTO));
    }

    /**
     * @description:查询角色和岗位信息
     * @author: yanhongwei
     * @param: []
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     **/
    @GetMapping
    public Map<String, Object> findRoleAndPost(){
        return Result.successMap(sysuserService.findRoleAndPost());
    }

    /**
     * @description:添加员工
     * @author: yanhongwei
     * @param: [sysUserDTO]
     * @return: com.itheima.huikebackend.common.Result<?>
     **/
    @PostMapping
    public Result<?> addUser(@RequestBody SysUserDTO sysUserDTO){
        sysuserService.addUser(sysUserDTO);
        return Result.success();
    }

    /**
     * @description:删除员工
     * @author: yanhongwei
     * @param: [id]
     * @return: com.itheima.huikebackend.common.Result<?>
     **/
    @DeleteMapping("/{id}")
    public Result<?> deleteUser(@PathVariable Long id){
        sysuserService.deleteUser(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<SysUserEchoVO > selectUser(@PathVariable String id){
        return Result.success(sysuserService.findUserById(id));
    }

    @PutMapping("/resetPwd")
    public Result<?> updateUserPassword(@RequestBody SysUserUpdateDTO sysUserUpdateDTO){
        sysuserService.updateUserPassword(sysUserUpdateDTO);
        return Result.success();
    }

    /**
     * @description:用户导出
     * @author: yanhongwei
     * @param: []
     * @return: com.itheima.huikebackend.common.R<com.itheima.huikebackend.common.PageResult>
     **/
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        sysuserService.export(response);
    }

    public R<PageResult> importUser(){
        return null;
    }
}
